﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;

namespace Ewts.Storage
{
    public partial class LoadNumByCustomerList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string username = "";
               //int cumid = 0;
                if (Session["UserInfo"] != null)
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User) Session["UserInfo"];
                    username = curUser.UserName;
                    hidusrName.Value = username;
                    //cumid = int.Parse(Request["cumid"].ToString().Trim());
                    ListItem item = new ListItem();
                    item.Text = "--请选择--";
                    item.Value = "";
                    DataLib.BLL.City bllLib = new DataLib.BLL.City();
 

                    dropGraphic.DataSource = bllLib.GetList(" flag1 ='02'");
                    dropGraphic.DataTextField = "cityname";
                    dropGraphic.DataValueField = "cityname";
                    dropGraphic.DataBind();
                    dropGraphic.Items.Insert(0, item);

                    if (curUser.UserType != "AD")
                    {
                        dropGraphic.Enabled = false;
                        dropGraphic.SelectedValue = curUser.CompanyCity;
                        //    bindData(dropGraphic.SelectedValue);
                    }
                    else
                    {
                        //   bindData("");
                    }

                    if (!string.IsNullOrEmpty(Request["LoadID"]))
                    {
                        CancleLoadNum(Convert.ToInt32(Request["LoadID"]));
                       
                        
                    }
                }
                else
                {
                    Response.Redirect("/accounts/login.aspx");
                }
                //  bindData(username);
            }
        }

        public void bindData(string username)
        {
            //DataTable dt = new DataTable();
            //dt = new DataLib.BLL.ProjectsRelation().getAddressByUserName(username, 6,0);
            //reptCustomer.DataSource = dt;
            //reptCustomer.DataBind();
        }

        /// <summary>
        /// 取得业务员真实姓名或者公司名称 by 2013.04.03  梁玲
        /// </summary>
        /// <param name="customerid"></param>
        /// <param name="issaleman">是否业务员</param>
        /// <returns></returns>
        protected string GetName(string id,int issaleman)
        {
            DataLib.Model.Cumtomer cummodel = new DataLib.Model.Cumtomer();
            cummodel = new DataLib.BLL.Cumtomer().GetModel(int.Parse(id));
            if (issaleman == 0)
            {
                string name = "";
                try
                {
                    DataLib.Model.LoadNum lm = new DataLib.BLL.LoadNum().GetModel(Convert.ToInt32(id)); //换成根据loadnum获取且加了异常处理 2013.04.03 梁玲
                    name = new User(lm.saleman).TrueName;
                }
                catch (Exception)
                {
                   
                }
                return name;
            }
            else
                return cummodel.companyname;

        }

        public string GetAddress(string addressid)
        {
            DataLib.Model.CumtomerAddress addmodel = new DataLib.Model.CumtomerAddress();
            addmodel = new DataLib.BLL.CumtomerAddress().GetModel(int.Parse(addressid));
            return addmodel.address;
        }

        protected void reptCustomer_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "作废")
            {
                DataLib.Model.LoadNum loadModel = new DataLib.BLL.LoadNum().GetModel(int.Parse(e.CommandArgument.ToString()));
                loadModel.status = -9;
                new DataLib.BLL.LoadNum().Update(loadModel);

                DataTable dt = new DataLib.BLL.Cjo().GetCJOList(loadModel.id.ToString());
                DataLib.BLL.Cjo bllCjo = new DataLib.BLL.Cjo();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataLib.Model.Cjo cjomodel = new DataLib.BLL.Cjo().GetModel(int.Parse(dt.Rows[i]["id"].ToString()));
                        cjomodel.status = -9;
                        bllCjo.Update(cjomodel);
                    }
                }

                DataLib.Common.MessageBox.Show(Page, "已经作废");

                string username = "";
               //int cumid = 0;
                
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    username = curUser.UserName;

                  //  bindData(username);
            }
        }

        protected bool GetEnable(string c_status)
        {
            int cstatus = int.Parse(c_status);
            bool flag = true;
            if (cstatus > 3) {
                flag = false;
            }
            return flag;
        }

        protected void reptCustomer_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            LinkButton link1 = (LinkButton)e.Item.FindControl("linkCanel");
            if (link1 != null)
            {
                if (link1.Enabled == true)
                {
                    link1.ForeColor = System.Drawing.Color.Blue; ;
                }
                else
                {
                    link1.ForeColor = System.Drawing.Color.Gray;
                }
            }
        }

        /// <summary>
        /// 取得当前状态的中文描述
        /// </summary>
        /// <param name="loadid"></param>
        /// <returns></returns>
        protected string GetStatus(string loadid)
        {
            string statusname = "";
            
            DataTable dt = new DataLib.BLL.Cjo().GetCJOList(loadid);
            DataLib.BLL.Cjo bllCjo = new DataLib.BLL.Cjo();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataLib.Model.Cjo cjomodel = new DataLib.BLL.Cjo().GetModel(int.Parse(dt.Rows[i]["id"].ToString()));
                    switch (cjomodel.status)
                    {
                        case 1:
                            statusname = "待派车";
                            break;
                        case 2:
                            statusname = "待称重";
                            break;
                        case 3:
                            statusname = "待授权";
                            break;
                        case 4:
                            statusname = "待拆解";
                            break;
                        case 5:
                            statusname = "拆解中";
                            break;
                        case -4:
                            statusname = "拆解申请审批中";
                            break;
                        case -9:
                            statusname = "作废";
                            break;
                        default:
                            break;
                    }
                }
            }

            return statusname;

        }



        protected void CancleLoadNum(int loadid)
        {
            DataLib.Model.LoadNum loadModel = new DataLib.BLL.LoadNum().GetModel(loadid);
            loadModel.status = -9;
            new DataLib.BLL.LoadNum().Update(loadModel);

            DataTable dt = new DataLib.BLL.Cjo().GetCJOList(loadModel.id.ToString());
            DataLib.BLL.Cjo bllCjo = new DataLib.BLL.Cjo();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataLib.Model.Cjo cjomodel = new DataLib.BLL.Cjo().GetModel(int.Parse(dt.Rows[i]["id"].ToString()));
                    cjomodel.status = -9;
                    bllCjo.Update(cjomodel);
                }
            }

            Response.Write("<script>alert('已作废');window.location.href='/Storage/LoadNumByCustomerList.aspx';</script>");
            
              
            }
        }
    }
